package yhp.com.zijie.web;

import yhp.com.zijie.bean.Grade;
import yhp.com.zijie.bean.Student;
import yhp.com.zijie.service.GradeService;
import yhp.com.zijie.service.StudentService;
import yhp.com.zijie.service.impl.GradeServiceImpl;
import yhp.com.zijie.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


/**
 * 多个请求路径合并，只需要在前端页面的请求路径上加上method=[路径名]
 */
@WebServlet("/Educational/student/studentServlet")
public class StudentServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");

        if ("addStudent".equals(method)){
            addStudent(req, resp);
        }else if ("updateStudent".equals(method)){
            updateStudent(req, resp);
        }else if ("deleteStudent".equals(method)){
            deleteStudent(req, resp);
        }else if ("findById".equals(method)){
            findById(req, resp);
        }else {
            studentList(req, resp);
        }
    }



    protected void addStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");//
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");


        System.out.println("===================");
        System.out.println(politics);

        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);


        StudentService studentService = new StudentServiceImpl();
        int i = studentService.addStudent(student);

        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('添加成功');location.href='/Educational/student/studentServlet'</script>");
        }else {
            writer.println("<script>alert('添加失败');location.href='/Educational/student/gradeList'</script>");
        }

    }

    protected void studentList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取查询参数
        //获取查询参数
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        System.out.println(stuname);
        System.out.println(stuno);
        System.out.println(sex);

        //获取前端的当前页码
        String pageIndex = req.getParameter("pageIndex");

        //默认是第一页
        int index=pageIndex==null?1:Integer.parseInt(pageIndex);
        //如果没选择性别查询，则默认-1
        int usex=(sex==null||sex.length()==0?-1:Integer.parseInt(sex));


        //调用方法，获取学生信息
        StudentService service = new StudentServiceImpl();
        List<Student> allStudent = service.getAllStudent(stuname , stuno , usex , index , 5);


        //获取总数
        int total = service.total(stuname, stuno,usex);
        //通过总数得到总页码数
        int totalPages=total%5>0?total/5+1:total/5;

        //存储dao层查询出的所有学生信息
        req.setAttribute("allStudent" , allStudent);
        //存储前端查询的传来的值
        req.setAttribute("stuname",stuname);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",sex);
        //存储分页相关信息
        req.setAttribute("index",index);
        req.setAttribute("total",total);
        req.setAttribute("totalPages",totalPages);

        //跳转页面
        req.getRequestDispatcher("list.jsp").forward(req,resp);

    }

    protected void updateStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String sid = req.getParameter("sid");
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");

        System.out.println("----------------------------");
        System.out.println(sid);
        System.out.println(stuName);
        System.out.println(gid);
        System.out.println(sex);


        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);
        student.setStuId(Integer.parseInt(sid));


        StudentService studentService = new StudentServiceImpl();
        int i = studentService.updateStudent(student);

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();

        if (i>0){
            writer.println("<script>alert('更新成功');location.href='/Educational/student/studentServlet'</script>");
        }else {
            writer.println("<script>alert('更新失败');location.href='/Educational/student/studentServlet?method=findById&sid="+sid+"'</script>");
        }

    }

    protected void deleteStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuId = req.getParameter("stuId");

        StudentService studentService = new StudentServiceImpl();
        int i = studentService.deleteById(Integer.parseInt(stuId));

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();

        if (i>0){
            writer.println("<script>alert('删除成功');location.href='/Educational/student/studentServlet'</script>");
        }else {
            writer.println("<script>alert('删除失败');location.href='/Educational/student/studentServlet'</script>");
        }
    }

    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuId = req.getParameter("stuId");
        System.out.println(stuId);
        System.out.println(".....................................");

        StudentService studentService = new StudentServiceImpl();
        //查询学生信息
        Student student = studentService.findById(Integer.parseInt(stuId));

        //
        GradeService gradeService = new GradeServiceImpl();
        List<Grade> gradeList = gradeService.showGrade();

        req.setAttribute("stu", student);
        req.setAttribute("grade" , gradeList);

        //跳转页面
        req.getRequestDispatcher("edit.jsp").forward(req , resp);
    }



}
